package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;
    @RequestMapping("addToCart.do")
    @ResponseBody
    public ServerResponse addToCart(HttpSession session,Integer productId, Integer count){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        return  cartService.addProductToCart(user.getId(),productId,count);
    }

    @RequestMapping("updateCart.do")
    @ResponseBody
    public ServerResponse updateCart(HttpSession session,Integer productId, Integer count){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        return  cartService.updateCart(user.getId(),productId,count);
    }
    @RequestMapping("deleteCartById.do")
    @ResponseBody
    public ServerResponse deleteCartById(HttpSession session,String  productIds){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        return  cartService.deleteCart(user.getId(),productIds);
    }
   //全选
    @RequestMapping("selectCheckAll.do")
    @ResponseBody
    public ServerResponse selectCheckAll(HttpSession session,Integer checked){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        checked=1;
        return  cartService.updateCheckedOneOrAll(user.getId(),null,checked);
    }
    //全反选
    @RequestMapping("unSelectCheckAll.do")
    @ResponseBody
    public ServerResponse unSelectCheckAll(HttpSession session,Integer checked){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        checked=0;
        return  cartService.updateCheckedOneOrAll(user.getId(),null,checked);
    }
    //单选
    @RequestMapping("checkOne.do")
    @ResponseBody
    public ServerResponse checkOne(HttpSession session,Integer productId,Integer checked){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        checked=1;
        return  cartService.updateCheckedOneOrAll(user.getId(),productId,checked);
    }
    //单不选
    @RequestMapping("uncheckOne.do")
    @ResponseBody
    public ServerResponse uncheckOne(HttpSession session,Integer productId,Integer checked){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return ServerResponse.createServerResponseByFailure(ResponseCode.NEED_LOGIN.getStatus(),"请登陆");
        }
        checked=0;
        return  cartService.updateCheckedOneOrAll(user.getId(),productId,checked);
    }

}
